﻿using IHISCETeim.BLL;
using IHISCETeim.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using IHISCETeim.Library;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
namespace IHISCETeim.Web.AgentSys.VipMgr
{
    public partial class VIPCustomerExchangeAdd : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.VIP积分兑换_添加 };
            }
        }
        protected override string DWZRef
        {
            get { return "VIPCustomerList"; }

        }


        protected int CustomerId
        {
            get
            {
                return GetRequestValue<int>("CustomerId", 0);
            }
        }
        private VIPCustomerInfo _vipObj = null;
        protected VIPCustomerInfo vipObj
        {
            get
            {
                if (_vipObj == null)
                    _vipObj = VIPCustomerController.GetList(CustomerId);

                return _vipObj;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                add();//
            }
        }

        private void add()
        {
            VIPCustomerExchangeInfo obj = Request.SerializableObject<VIPCustomerExchangeInfo>();
            obj.UserId = vipObj.UserId;
            obj.AgentName = vipObj.AgentName;
            
           // obj.AgentTotalPoint = obj.TotalPoint;


            char[] caSplit = new char[] { ',' };
            string[] ItemCodes = GetRequestValue<string>("ItemCode", "").Split(caSplit);
            string[] ItemNames = GetRequestValue<string>("ItemName", "").Split(caSplit);
            string[] Prices = GetRequestValue<string>("Price", "").Split(caSplit);
            string[] Quantitys = GetRequestValue<string>("Quantity", "").Split(caSplit);
            string[] LineTotals = GetRequestValue<string>("LineTotal", "").Split(caSplit);
            string[] LinePoints = GetRequestValue<string>("LinePoint", "").Split(caSplit);

            List<VIPCustomerExchangeDetailInfo> list = new List<VIPCustomerExchangeDetailInfo>();
            for (int rowIndex = 0; rowIndex < ItemCodes.Count(); rowIndex++)
            {
                VIPCustomerExchangeDetailInfo itm = new VIPCustomerExchangeDetailInfo();
                itm.ItemCode = ItemCodes[rowIndex];
                itm.ItemName = ItemNames[rowIndex];
                itm.Price = Convert.ToDecimal(Prices[rowIndex]);
                itm.Quantity = Convert.ToDecimal(Quantitys[rowIndex]);
                itm.LineTotal = Convert.ToDecimal(LineTotals[rowIndex]);
                itm.LinePoint = Convert.ToInt32(LinePoints[rowIndex]);
                list.Add(itm);
            }


            int R = VIPCustomerExchangeController.Add(obj, list);
            switch (R)
            {
                case 1:
                    SuccessJson("已成功添加VIP客户积分兑换信息.");
                    break;
                //case 2:
                //    SuccessJson("已成功添加VIP配件采购信息");
                //    break;
                case 0:
                    ErrorJson("添加添加VIP客户积分兑换信息失败");
                    break;
            }
        }
    }
}